package screens;

import view.WorldRenderer;
import model.World;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.me.mygdxgame.Gamedoan;

import controller.BobController;
import controller.WorldController;

public class GameMainScreen implements Screen, InputProcessor{
	public enum StateGame{
		GAMERUNNING,GAMEOVER,GAMEPAUSE,GAMEEND
	}
	World world;
	WorldRenderer renderer;
	WorldController controller;
	Gamedoan myGame;
	private int width, height;
	public GameMainScreen(Gamedoan game){
		myGame=game;
	}
	public GameMainScreen(){
		
	}
	@Override
	public void show(){
		world = new World();
		renderer = new WorldRenderer(world,false);
		controller =new WorldController(world);
		Gdx.input.setInputProcessor((InputProcessor) this);
	}
	@Override
					
	public void render(float delta) {
		// TODO Auto-generated method stub
		Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		controller.update(delta);
		renderer.render();
		
		if(controller.getStateGame().equals(StateGame.GAMEEND))
		{
			//set screen
			myGame.setScreen(new EndGameScreen(myGame));
			this.dispose();
		}
	}
	@Override 
	public void resize (int width, int height){
		renderer.setSize(width, height);
		this.width=width;
		this.height=height;
	}

	@Override 
	public void hide(){
		Gdx.input.setInputProcessor(null);
	}
	@Override
	public void pause(){
		
		
	}
	@Override
	public void resume(){
		
	}
	@Override
	public void dispose(){
		Gdx.input.setInputProcessor(null);
	}
	///-------input processor methods------////
	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		if(keycode == Keys.LEFT)
			controller.leftPressed();
		if(keycode==Keys.RIGHT)
			controller.rightPressed();
		if(keycode==Keys.Z)
			controller.jumpPressed();
		if(keycode==Keys.X)
			controller.firePressed();
		
		return true;
	}
	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		if(keycode == Keys.LEFT)
			controller.leftReleased();
		if(keycode==Keys.RIGHT)
			controller.rightReleased();
		if(keycode==Keys.Z)
			controller.jumpReleased();
		if(keycode==Keys.X)
			controller.fireReleased();
		if(keycode==Keys.D)
			renderer.setDebug(!renderer.isDebug());
		return true;
	}
	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		if(!Gdx.app.getType().equals(ApplicationType.Android))
			return false;
		if(screenX<width/2&&screenY>height/2){
			controller.leftPressed();
			controller.rightReleased();
		}
		else if(screenX>width/2&&screenY>height/2){
			controller.rightPressed();
			controller.leftReleased();
		}
		if(screenX>width/2&&screenY<height/2){
			controller.jumpPressed();
		}
		if(screenX<width/2&&screenY<height/2){
			controller.firePressed();
		}
		return true;
	}
	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		if(!Gdx.app.getType().equals(ApplicationType.Android))
			return false;
		if(screenX<width/2&&screenY>height/2)
			controller.leftReleased();
		if(screenX>width/2&&screenY>height/2)
			controller.rightReleased();
		if(screenX>width/2&&screenY<height/2){
			controller.jumpReleased();
		}
		if(screenX<width/2&&screenY<height/2){
			controller.fireReleased();
		}
		return true;
	}
	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		
		return false;
	}
	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}
	
}
